/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.hycreate.platform.modules.crm.project;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hycreate.platform.common.utils.StringUtils;
import com.hycreate.platform.common.web.BaseController;
import com.hycreate.platform.modules.crm.business.CrmSysDict;
import com.hycreate.platform.modules.crm.business.Customer;
import com.hycreate.platform.modules.crm.business.CustomerService;
import com.hycreate.platform.modules.sys.entity.Office;
import com.hycreate.platform.modules.sys.entity.User;
import com.hycreate.platform.modules.sys.utils.UserUtils;

/**
 * 项目管理Controller
 * 
 * @author hj
 * @version 2016-12-02
 */
@Controller
@RequestMapping(value = "${adminPath}/crm/project/project")
public class ProjectController extends BaseController {
	ObjectMapper mapper = new ObjectMapper();
	@Autowired
	private ProjectService projectService;
	@Autowired
	private ProjectContactService projectContactService;
	@Autowired
	private CustomerService customerService;
	@Autowired
	private HycrmRemindService hycrmRemindService;

	@ModelAttribute
	public Project get(@RequestParam(required = false) String id) {
		Project entity = null;
		if (StringUtils.isNotBlank(id)) {
			entity = projectService.get(id);
		}
		if (entity == null) {
			entity = new Project();
		}
		return entity;
	}

	/**
	 * 查找项目和公司名项目名
	 * 
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequiresPermissions("project:project:view")
	@RequestMapping(value = { "list", "" })
	public String list(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		/*//查询所有客户
		List<Customer> customer = projectService.findAllCustomer();*/
		//查询跟进人是登录用户的所有项目
		List<Project> project = projectService.findAllProject(user);
		//查询所有项目名
		List<Project> projects =projectService.findProjectName();
		model.addAttribute("page", project);
		model.addAttribute("projects", projects);
		/*model.addAttribute("customer", customer);*/
		return "modules/crm/project/projectList";
	}
	/**
	 * 查询符合条件的项目
	 * @param project
	 * @param model
	 * @return
	 */
	@RequiresPermissions("project:project:findProject")
	@RequestMapping(value = "findProject")
	public String findProject(Project project,Model model){
		User user = UserUtils.getUser();
		project.setFollowMan(user.getId());
		//查询所有符合条件的项目
		List<Project> projects=projectService.findProject(project);
		/*//查询所有客户
		List<Customer> customer = projectService.findAllCustomer();*/
		//查询所有项目名
		List<Project> projectName =projectService.findProjectName();
		model.addAttribute("projects", projectName);
	/*	model.addAttribute("customer", customer);*/
		model.addAttribute("page",projects);
		return "modules/crm/project/projectList";
	}
	/**
	 * 查询单个项目信息
	 * @param project
	 * @param model
	 * @return
	 */
	@RequiresPermissions("project:project:edit")
	@RequestMapping(value = "form")
	public String form(Project project, Model model) {
		//查询所有部门
		List<Office> dept=projectService.findAllDept();
		//查询该项目的所有联系记录
		List<ProjectContact> projectContact=projectContactService.findAllProjectContact(project);
		//查询项目信息
		project = projectService.get(project.getId());
		String address=project.getFindCustomer().getAddress();
		address=address.substring(0, address.length()-1);
		project.getFindCustomer().setAddress(address);
		//查询项目所在公司的信息
		Customer cust = customerService.get(project.getCustomerId());
		//项目所在公司的行业
		List<CrmSysDict> sysDict =projectService.findDict(cust);  //查询项目所在公司的行业
		model.addAttribute("sysDict",sysDict);
		model.addAttribute("project", project);
		model.addAttribute("dept", dept);
		model.addAttribute("projectContact", projectContact);
		return "modules/crm/project/projectForms";
	}
	
	@RequiresPermissions("project:project:view")
	@RequestMapping(value = "delete")
	public String delete(Project project,RedirectAttributes redirectAttributes){
		projectService.delete(project);
		addMessage(redirectAttributes,"项目转移成功");
		return "redirect:" + adminPath + "/crm/project/project/list";
	}

	

	/**
	 * 根据部门查询部门下所有员工
	 * @param request
	 * @param response
	 * @param model
	 * @param redirectAttributes
	 * @throws IOException
	 */
	@RequiresPermissions("project:project:projectShift")
	@RequestMapping(value = "findUser")
	public void findUser(String deptId,HttpServletRequest request,
			HttpServletResponse response,Model model,
			RedirectAttributes redirectAttributes) throws IOException {
		    String deptIds=request.getParameter("deptId");
		    //通过部门Id查询部门下的员工
		    List<User>user=projectService.findUser(deptIds);
		    String customersJson = mapper.writeValueAsString(user);
			response.setContentType("application/json;charset=utf-8");
			response.getWriter().print(customersJson);
		  
	}
	/**
	 * 项目转移
	 * @param project
	 * @param followMan
	 * @param model
	 * @return
	 */
	@RequiresPermissions("project:project:projectShift")
	@RequestMapping(value = "projectShift")
	public String projectShift(Project project,String followMan,Model model,RedirectAttributes redirectAttributes){
		//更新项目跟进人
		projectService.save(project);
		//项目Id
		project.setpId(project.getId());
		HycrmRemind remind =new HycrmRemind();
		remind.setProjectId(project.getId());
		remind.setRemindTo(project.getFollowMan());
		//插入一条回馈信息
		hycrmRemindService.saveReminds(remind);
		addMessage(redirectAttributes,"项目转移成功");
		return "redirect:" + adminPath + "/crm/project/project/list";
	}

}